package com.csu.letaobackend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.csu.letaobackend.bean.Coupon;
import com.csu.letaobackend.bean.Order;
import com.csu.letaobackend.bean.Receiving;
import com.csu.letaobackend.commom.CommonResponse;
import com.csu.letaobackend.persistence.CouponDao;
import com.csu.letaobackend.persistence.ShopDao;
import com.csu.letaobackend.service.CouponService;
import com.csu.letaobackend.vo.CouponVO;
import com.csu.letaobackend.vo.OrderVO;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@MapperScan("com.csu.letaobackend.persistence")
public class CouponServiceImpl implements CouponService {

      @Autowired
      CouponDao couponDao;

      @Autowired
      ShopDao shopDao;


      @Override
      public CommonResponse<List<CouponVO>> getCouponsByUsername(String username) {
            List<CouponVO> couponVOList = new ArrayList<>();
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("username", username);

            List<Coupon> couponList = couponDao.selectList(queryWrapper);
            for (Coupon coupon : couponList) {
                  couponVOList.add(entityToVO(coupon));
            }
            return CommonResponse.createForSuccess(couponVOList);
      }


      private CouponVO entityToVO(Coupon coupon) {
            CouponVO couponVO = new CouponVO();
            couponVO.setCouponid(coupon.getCouponid());
            couponVO.setUsername(coupon.getUsername());
            couponVO.setShopid(coupon.getShopid());
            couponVO.setShopname(shopDao.selectById(coupon.getShopid()).getName());
            couponVO.setFullminus(coupon.getFullminus());
            couponVO.setDiscount(coupon.getDiscount());
            return couponVO;
      }
}
